import config from "@/config";


export default {
    uploadFile(file,progress){
        return new Promise((resolve,reject)=>{
            var xhr, formData;
            xhr = new XMLHttpRequest();
            if ("function"==typeof progress){
                xhr.upload.onprogress = function (e) {
                    progress(e.loaded / e.total * 100);
                }
            }
            xhr.withCredentials = false;
            xhr.open('POST', "api/upload"/*FileController 里面的路径*/);
            xhr.onload = function () {
                if (xhr.status !== 200) {
                    reject('HTTP Error: ' + xhr.status);
                }
                let result = JSON.parse(xhr.responseText);
                if (result.success) {
                    //第一个参数是可访问的图片url路径
                    resolve(config.imgBaseUrl+result.data[0])
                } else {
                    reject(result.message)
                }
            };
            formData = new FormData();
            formData.append('file', file, file.name);
            xhr.send(formData);
        })
    }
}
